﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.LIB;

namespace HangerSupport
{
	public partial class DlgHSCount : System.Windows.Forms.Form
	{
		public DlgHSCount()
		{
			this.InitializeComponent();
			YJKProcessTools hyprocessTools = new YJKProcessTools();
			hyprocessTools.ShowInTaskbarChange(this);
		}

		public DlgHSCount(UIApplication uiApp, List<HSCalResult> listTolHS, DlgHSCount.ViewType viewType)
		{
			this.InitializeComponent();
			YJKProcessTools hyprocessTools = new YJKProcessTools();
			hyprocessTools.ShowInTaskbarChange(this);
			this.m_uiApp = uiApp;
			this.m_doc = this.m_uiApp.ActiveUIDocument.Document;
			this.m_selection = this.m_uiApp.ActiveUIDocument.Selection;
			this.m_listTolHS = listTolHS;
			this.m_kCountType = DlgHSCount.CountTYPE.eCountAll;
			this.m_kViewType = viewType;
			bool flag = this.m_kViewType == (int)DlgHSCount.ViewType.eViewTypeMat;
			if (flag)
			{
				this.radioHSMat.Select();
			}
			else
			{
				this.radioHSNumber.Select();
			}
			listTolHS.Sort(new HSResultSortCompare());
			HSResultSortCompare hsresultSortCompare = new HSResultSortCompare();
			for (int i = 0; i < this.m_listTolHS.Count - 1; i++)
			{
				HSCalResult curCal = this.m_listTolHS[i];
				for (int j = i + 1; j < this.m_listTolHS.Count; j++)
				{
					HSCalResult otherOrder = this.m_listTolHS[j];
					bool flag2 = hsresultSortCompare.Compare(curCal, otherOrder) == 0;
					if (flag2)
					{
						this.m_listTolHS[i].m_nSameNum += 1L;
						this.m_listTolHS.RemoveAt(j);
						j--;
					}
				}
			}
			this.InitList();
		}

		private void InitList()
		{
			bool flag = this.m_kViewType == (int)DlgHSCount.ViewType.eViewTypeMat;
			bool flag2 = flag;
			if (flag2)
			{
				this.ListMatInfo();
			}
			else
			{
				this.ListNumberInfo();
			}
		}

		private void ListMatInfo()
		{
			this.listResult.Items.Clear();
			this.listResult.Columns.Clear();
			List<string> list = new List<string>();
			list.Add("支吊架类别");
			list.Add("相同数量");
			list.Add("支吊架尺寸");
			list.Add("型材类型");
			list.Add("型材规格");
			list.Add("相同个数");
			list.Add("单个长度(m)");
			list.Add("总长度(m)");
			this.m_listResultString.Clear();
			this.m_listResultString.Add(list);
			foreach (string text in list)
			{
				ColumnHeader columnHeader = new ColumnHeader();
				columnHeader.Text = text;
				columnHeader.Width = 102;
				bool flag = text == "相同个数";
				if (flag)
				{
					columnHeader.Width = 90;
				}
				this.listResult.Columns.Add(columnHeader);
			}
			string a = "";
			long num = 0L;
			long num2 = 0L;
			double num3 = 0.0;
			for (int i = 0; i < this.m_listTolHS.Count; i++)
			{
				HSCalResult hscalResult = this.m_listTolHS[i];
				string text2 = hscalResult.m_strHSName;
				bool flag2 = a == hscalResult.m_strHSName;
				if (flag2)
				{
					text2 = "";
				}
				else
				{
					a = text2;
				}
				num += hscalResult.m_nSameNum;
				ListViewItem listViewItem = new ListViewItem(text2);
				listViewItem.SubItems.Add(hscalResult.m_nSameNum.ToString());
				listViewItem.SubItems.Add(hscalResult.m_strSizeInfo);
				listViewItem.SubItems.Add("");
				listViewItem.SubItems.Add("");
				listViewItem.SubItems.Add("");
				listViewItem.SubItems.Add("");
				listViewItem.SubItems.Add("");
				this.listResult.Items.Add(listViewItem);
				for (int j = 0; j < hscalResult.m_lstBarList.Count; j++)
				{
					HSCalBarInfo hscalBarInfo = hscalResult.m_lstBarList[j];
					string str = "横担";
					bool flag3 = 1 == hscalBarInfo.m_nBarType;
					if (flag3)
					{
						str = "竖杆";
					}
					else
					{
						bool flag4 = 2 == hscalBarInfo.m_nBarType;
						if (flag4)
						{
							str = "斜杆";
						}
					}
					double num4 = hscalBarInfo.m_dLength / 1000.0;
					listViewItem = new ListViewItem();
					listViewItem.SubItems.Add("");
					listViewItem.SubItems.Add("");
					listViewItem.SubItems.Add(str + hscalBarInfo.m_matInfo.m_strName);
					listViewItem.SubItems.Add(hscalBarInfo.m_matInfo.GetSizeInfo());
					listViewItem.SubItems.Add(hscalBarInfo.m_nSameNum.ToString());
					listViewItem.SubItems.Add(num4.ToString("f3"));
					listViewItem.SubItems.Add(((double)hscalBarInfo.m_nSameNum * num4).ToString("f3"));
					this.listResult.Items.Add(listViewItem);
					num2 += hscalBarInfo.m_nSameNum * hscalResult.m_nSameNum;
					num3 += (double)(hscalBarInfo.m_nSameNum * hscalResult.m_nSameNum) * num4;
				}
			}
			ListViewItem listViewItem2 = new ListViewItem();
			listViewItem2.SubItems.Add("");
			listViewItem2.SubItems.Add("");
			listViewItem2.SubItems.Add("");
			listViewItem2.SubItems.Add("");
			listViewItem2.SubItems.Add("");
			listViewItem2.SubItems.Add("");
			listViewItem2.SubItems.Add("");
			this.listResult.Items.Add(listViewItem2);
			listViewItem2 = new ListViewItem("支吊架总数");
			listViewItem2.SubItems.Add(num.ToString());
			listViewItem2.SubItems.Add("");
			listViewItem2.SubItems.Add("");
			listViewItem2.SubItems.Add("杆件总数");
			listViewItem2.SubItems.Add(num2.ToString());
			listViewItem2.SubItems.Add("杆件总长");
			listViewItem2.SubItems.Add(num3.ToString("f3"));
			this.listResult.Items.Add(listViewItem2);
		}

		private void ListNumberInfo()
		{
			this.listResult.Items.Clear();
			this.listResult.Columns.Clear();
			List<string> list = new List<string>();
			list.Add("型材类型");
			list.Add("型材尺寸");
			list.Add("单个长度(m)");
			list.Add("个数");
			list.Add("总长度(m)");
			this.m_listResultString.Clear();
			this.m_listResultString.Add(list);
			foreach (string text in list)
			{
				ColumnHeader columnHeader = new ColumnHeader();
				columnHeader.Text = text;
				columnHeader.Width = 100;
				this.listResult.Columns.Add(columnHeader);
			}
			List<HSCalBarInfo> list2 = new List<HSCalBarInfo>();
			for (int i = 0; i < this.m_listTolHS.Count; i++)
			{
				HSCalResult hscalResult = this.m_listTolHS[i];
				for (int j = 0; j < hscalResult.m_lstBarList.Count; j++)
				{
					HSCalBarInfo item = new HSCalBarInfo(hscalResult.m_lstBarList[j]);
					item.m_nSameNum *= hscalResult.m_nSameNum;
					list2.Add(item);
				}
			}
			list2.Sort(new HSBarMatSortCompare());
			bool flag = list2.Count == 0;
			if (!flag)
			{
				for (int k = 0; k < list2.Count - 1; k++)
				{
					for (int l = k + 1; l < list2.Count; l++)
					{
						bool flag2 = list2[l].IsSameSizeLength(list2[k]);
						if (flag2)
						{
							HSCalBarInfo hscalBarInfo = list2[k];
							hscalBarInfo.m_nSameNum += list2[l].m_nSameNum;
							list2[k] = hscalBarInfo;
							list2.RemoveAt(l);
							l--;
						}
					}
				}
				HSCalBarInfo hscalBarInfo2 = new HSCalBarInfo(list2[0]);
				hscalBarInfo2.m_matInfo.m_strName = "";
				double num = 0.0;
				long num2 = 0L;
				double num3 = 0.0;
				long num4 = 0L;
				bool flag3 = false;
				for (int m = 0; m < list2.Count; m++)
				{
					HSCalBarInfo hscalBarInfo3 = list2[m];
					string text2 = hscalBarInfo3.m_matInfo.GetSizeInfo();
					bool flag4 = hscalBarInfo2.m_matInfo.m_strName == hscalBarInfo3.m_matInfo.m_strName;
					if (flag4)
					{
						bool flag5 = hscalBarInfo2.IsSameSize(hscalBarInfo3);
						if (flag5)
						{
							text2 = "";
						}
						hscalBarInfo3.m_matInfo.m_strName = "";
					}
					else
					{
						hscalBarInfo2 = list2[m];
					}
					double num5 = hscalBarInfo3.m_dLength / 1000.0;
					ListViewItem listViewItem = new ListViewItem(hscalBarInfo3.m_matInfo.m_strName);
					listViewItem.SubItems.Add(text2);
					listViewItem.SubItems.Add(num5.ToString("f3"));
					listViewItem.SubItems.Add(hscalBarInfo3.m_nSameNum.ToString());
					listViewItem.SubItems.Add((num5 * (double)hscalBarInfo3.m_nSameNum).ToString("f3"));
					this.listResult.Items.Add(listViewItem);
					num2 += hscalBarInfo3.m_nSameNum;
					num += num5 * (double)hscalBarInfo3.m_nSameNum;
					bool flag6 = m == list2.Count - 1;
					if (flag6)
					{
						flag3 = true;
					}
					else
					{
						HSCalBarInfo hscalBarInfo4 = list2[m + 1];
						bool flag7 = hscalBarInfo4.m_matInfo.m_strName != list2[m].m_matInfo.m_strName;
						if (flag7)
						{
							flag3 = true;
						}
						else
						{
							bool flag8 = !hscalBarInfo4.IsSameSize(list2[m]);
							if (flag8)
							{
								flag3 = true;
							}
						}
					}
					bool flag9 = flag3;
					if (flag9)
					{
						ListViewItem listViewItem2 = new ListViewItem();
						listViewItem2.SubItems.Add("相同型材合计");
						listViewItem2.SubItems.Add("");
						listViewItem2.SubItems.Add(num2.ToString());
						listViewItem2.SubItems.Add(num.ToString("f3"));
						this.listResult.Items.Add(listViewItem2);
						num3 += num;
						num4 += num2;
						num = 0.0;
						num2 = 0L;
						flag3 = false;
					}
				}
				ListViewItem listViewItem3 = new ListViewItem();
				listViewItem3.SubItems.Add("");
				listViewItem3.SubItems.Add("");
				listViewItem3.SubItems.Add("");
				listViewItem3.SubItems.Add("");
				this.listResult.Items.Add(listViewItem3);
				listViewItem3 = new ListViewItem("");
				listViewItem3.SubItems.Add("所有型材总计");
				listViewItem3.SubItems.Add("");
				listViewItem3.SubItems.Add(num4.ToString());
				listViewItem3.SubItems.Add(num3.ToString("f3"));
				this.listResult.Items.Add(listViewItem3);
			}
		}

		private void radioHSMat_CheckedChanged(object sender, EventArgs e)
		{
			this.m_kViewType = DlgHSCount.ViewType.eViewTypeMat;
			this.InitList();
		}

		private void radioHSNumber_CheckedChanged(object sender, EventArgs e)
		{
			this.m_kViewType = DlgHSCount.ViewType.eViewTypeNumber;
			this.InitList();
		}

		private void btnCountAll_Click(object sender, EventArgs e)
		{
			this.m_kCountType = DlgHSCount.CountTYPE.eCountAll;
			base.Close();
			base.DialogResult = DialogResult.OK;
		}

		private void btnCountSelect_Click(object sender, EventArgs e)
		{
			this.m_kCountType = DlgHSCount.CountTYPE.eCountSelect;
			base.Close();
			base.DialogResult = DialogResult.OK;
		}

		private void btnDrawResult_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.listResult.Items.Count; i++)
			{
				ListViewItem listViewItem = this.listResult.Items[i];
				List<string> list = new List<string>();
				for (int j = 0; j < listViewItem.SubItems.Count; j++)
				{
					string text = listViewItem.SubItems[j].Text;
					list.Add(text);
				}
				this.m_listResultString.Add(list);
			}
			this.m_kCountType = DlgHSCount.CountTYPE.eCountDraw;
			base.Close();
			base.DialogResult = DialogResult.OK;
		}

		public void DrawTable(XYZ ptBase)
		{
			Transaction transaction = new Transaction(this.m_doc, "DrawTable");
			transaction.Start();
			double num = HSFamily.mmToFeet(3100.0);
			double num2 = HSFamily.mmToFeet(1000.0);
			HSResultDraw hsresultDraw = new HSResultDraw(this.m_uiApp);
			double num3 = 0.0;
			double num4 = 0.0;
			XYZ xyz = XYZ.BasisY * -1.0;
			foreach (List<string> list in this.m_listResultString)
			{
				num4 = 0.0;
				foreach (string strText in list)
				{
					hsresultDraw.DrawCell(ptBase + XYZ.BasisX * num4 + xyz * num3, num, num2, strText);
					num4 += num;
				}
				num3 += num2;
			}
			ptBase += XYZ.BasisX * num4 + xyz * num3;
			hsresultDraw.DrawModelLine(ptBase, -xyz * num3);
			hsresultDraw.DrawModelLine(ptBase, -XYZ.BasisX * num4);
			hsresultDraw.CreateGroup();
			transaction.Commit();
		}

		private UIApplication m_uiApp;

		private Document m_doc;

		private Selection m_selection;

		public DlgHSCount.CountTYPE m_kCountType;

		public DlgHSCount.ViewType m_kViewType;

		private List<HSCalResult> m_listTolHS;

		private List<List<string>> m_listResultString = new List<List<string>>();

		public enum CountTYPE
		{
			eCountAll,
			eCountSelect,
			eCountDraw
		}

		public enum ViewType
		{
			eViewTypeMat,
			eViewTypeNumber
		}
	}
}
